package com.demo.paypalmanagement.controller;


import cn.hutool.core.util.ObjectUtil;
import com.demo.paypalmanagement.model.entity.Users;
import com.demo.paypalmanagement.model.vo.Result;
import com.demo.paypalmanagement.service.IUsersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class ComController {

  @Autowired
  private IUsersService userService;

  @PostMapping("/com/updatePwd")
  public ResponseEntity updatePwd(@RequestParam("password") String pwd) {
    userService.changePassword(pwd);
    return ResponseEntity.ok("successfully");
  }
  @GetMapping("/com/checkLogin")
  public Result checkLogin() {
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    if("anonymousUser".equals(username)){
      return Result.fail(403, "not login");
    }
    Users userByUsername = userService.getUserByUsername(username);
    return Result.success(userByUsername.getAccount());
  }

  @GetMapping("/com/get")
  public Result get() {
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    if("anonymousUser".equals(username)){
      return Result.fail(403, "not login");
    }
    Users userByUsername = userService.getUserByUsername(username);
    Users user = new Users();
    user.setAccount(userByUsername.getAccount());
    user.setId(userByUsername.getId());
    return Result.success(user);
  }
}
